<div class="admin-content">

  <div class="am-cf am-padding">
    <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">菜单管理</strong> / <small>导航菜单设置</small></div>
  </div>
  <hr/>
  <div class="am-g am-padding">
  	  <form class="am-form-inline am-fr" method="post" id="queryForm">
	      <div class="am-form-group">
	          <select name="type" id='queryMenuType'>
	            <option value="1">顶部导航</option>
	            <option value="2">左侧导航</option>
	          </select>
	      </div>
	      <div class="am-input-group am-input-group-sm">
	          <input type="text" class="am-form-field" name="keyword">
	          <span class="am-input-group-btn">
	            <button class="am-btn am-btn-default " type="button" id="queryBtn">查询</button>
	          </span>
	      </div>
	 </form>
	 <div class="am-u-sm-12 am-u-md-6 am-u-end">
	        <div class="am-btn-toolbar">
	          <div class="am-btn-group am-btn-group-sm">
	            <button type="button" class="am-btn" id="addBtn"><span class="am-icon-plus"></span> 新增</button>
	            <button type="button" class="am-btn" id="batchDelBtn"><span class="am-icon-trash-o" ></span> 删除</button>
	            <button type="button" class="am-btn"><span class="am-icon-gear"></span> 权限设置</button>
	          </div>
	        </div>
	 </div>
  </div>
    <div class="am-g">
      <div class="am-u-sm-12">
        <form class="am-form" id="dataForm" method="post">
          <table class="am-table am-table-striped am-table-hover table-main" id="amDataTable">
            <thead>
              <tr>
                <th class="" data-options="field:'amCk'" width="5%"><input type="checkbox" name='amCk'/></th>
                <th class="" data-options="field:'amNo'"  width="10%">序号</th>
                <th class="" data-options="field:'name',amClass:'am-text-truncat',formatter:formatter.icoName"  width="20%">名称</th>
                <th class=" am-hide-sm-only" data-options="field:'url'  ,amClass:' am-hide-sm-only'"  width="20%"> URL</th>
                <th class="" data-options="field:'isParent',formatter:formatter.isParent"  width="10%">父级菜单</th>
                <th class="" data-options="field:'sort'"  width="10%">排序号</th>
                <th class=" " data-options="field:'status',formatter:formatter.status "  width="8%">状态</th>
                <th class="" data-options="field:'opt',amClass:'am-align-center',formatter:formatter.opt"  >操作</th>
              </tr>
          </thead>
          
        </table>
        </form>
         
  </div>

</div>
<script type="text/javascript">
	var formatter = {
		icoName : function(value ,data ,index){
			return '<span class="'+data.icon+'"></span> '+value;
		},
		status : function(value, data, index){
			if(value == '1'){
				return '<span class="am-text-success">启用</span>';
			}else{return '<span class="am-text-warning">停用</span>'}
		},
		isParent : function(value, data, index){
			if(value){
				return '<span class="am-badge am-badge-primary">Yes<span>';
			}else{return '<span class="am-badge am-badge-warning">No</span>'}
		},
		opt : function(value, data, index){
			var ohtml = ' <div class="am-btn-toolbar">'  +
						'	<div class="am-btn-group am-btn-group-xs"> ' +
						'   <button type="button" class="am-btn am-btn-default am-btn-xs am-text-secondary" onclick="menuEdit('+index+')"><span class="am-icon-pencil-square-o"></span> 编辑</button> ' +
						'   <button type="button" class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only" onclick="delMenu('+index+')"><span class="am-icon-trash-o"></span> 删除</button> ' +
						'  </div></div>'
			return ohtml ;
		}
			
	};
	$('#queryMenuType').selectIt({btnSize:'sm',btnStyle:'default'});
	$("#amDataTable").amDataGrid({url:'sys/getMenus',pagination:true,queryParams:$('#queryForm').amFormToJson()});
	
	$('#queryBtn').click(function(){
		$("#amDataTable").amDataGrid("load",$('#queryForm').amFormToJson());
	});
	$("#addBtn").click(function(){
		amFun.includePage('sys/menu_edit');
	});
	$("#batchDelBtn").click(function(){
		$("#amDataTable").amDataGrid("getSelectRows" , function(rows){
			 var ids = ''; 
			 for ( var i in rows) {
				ids += rows[i].id + ',';
			 }
			 ids = ids == '' ? '' : ids.substring(0,ids.length-1);
			 if(ids == '' ) return ;
			 var flag = confirm("确定要删除指定菜单吗？"); 
			 if(flag){
			 	$.post('menu/batchDel', {ids:ids}, function(r) {
					if(r){
						$("#amDataTable").amDataGrid("refresh");
					}else{
						alert(r.m);
					}
			    }, 'json');
			 }
		});
	});
	function menuEdit(index){
		var rows = $('#amDataTable').amDataGrid("getRows");
		amFun.includePage('sys/menu_edit', rows[index]);
	}
	
	//删除指定菜单
	function delMenu(index){
		console.info("del _" +index);
		var flag = confirm("确认要删除该菜单吗？" );
		if(flag){
			var rows = $('#amDataTable').amDataGrid("getRows");
			$.post('menu/batchDel', {ids:rows[index].id}, function(r) {
				if(r){
					$("#amDataTable").amDataGrid("refresh");
				}else{
					alert(r.m);
				}
		    }, 'json');
		}
		
		
	}
</script>

<!-- content end -->
</div>